#pragma once
#include <drogon/drogon.h>
#include <drogon/WebSocketController.h>
#include <drogon/HttpAppFramework.h>
#include <drogon/WebSocketController.h>
#include <boost/asio.hpp>
#include <boost/locale.hpp>
#include <boost/algorithm/string.hpp>
#include "sensor.hpp"

#include <sstream>
#include <fstream>

using namespace drogon;
namespace iepe{
    static std::string capture_config[11][3] = {
        {"通道使能设置","\t1\t1\t1\t1\t\n","2",},
        {"采样率","\t0\n","1",},
        {"采样模式","\t0\n","1",},
        {"传输/记录","\t0\n","1",},
        {"单次触发采样次数","\t0\n","4",},
        {"周期触发参数","\t0\n","8",},
        {"事件触发参数","\t0\n","8",},
    };
    static std::string param_config[2][3] = {
        {"低通滤波频率","\t0\n","2",},
        {"通道量程/增益设置","\t0\n","4",},
    };
    void exports(const HttpRequestPtr& req,const HttpResponsePtr& resp,
        std::map<boost::asio::ip::tcp::socket*,std::tuple<std::string,bool>>::iterator it,
        std::function<void(int,int,std::vector<unsigned char>&)> callback){
        std::stringstream sconfig;
        std::ifstream ifs(std::get<0>(it->second)+".csv");
        if(!ifs.is_open()|| ifs.peek() == std::ifstream::traits_type::eof()){
            for(auto item:capture_config){
                sconfig<<boost::locale::conv::to_utf<char>(item[0],"utf8")<<item[1];
            }
            for(auto item:param_config){
                sconfig<<boost::locale::conv::to_utf<char>(item[0],"utf8")<<item[1];
            }
            std::ofstream ofs(std::get<0>(it->second)+".csv");
            ofs<<sconfig.str();
            ofs.close();
        }else{
            ifs>>sconfig.rdbuf();
        }
        ifs.close();
        resp->setBody(sconfig.str());
    }
    void set_capture(const HttpRequestPtr& req,const HttpResponsePtr& resp,
        std::map<boost::asio::ip::tcp::socket*,std::tuple<std::string,bool>>::iterator it,
        std::function<void(int,int,std::vector<unsigned char>&)> callback){
        std::vector<unsigned char> data,config;
        for(auto item:capture_config){
            config = config_parse(item[0].c_str(),req->getParameter(item[0]),atoi(item[2].c_str()));
            data.insert(data.end(),config.begin(),config.end());
        }
        callback(60,0x40,data);
    }
    void set_sensor(const HttpRequestPtr& req,const HttpResponsePtr& resp,
        std::map<boost::asio::ip::tcp::socket*,std::tuple<std::string,bool>>::iterator it,
        std::function<void(int,int,std::vector<unsigned char>&)> callback){
        std::vector<unsigned char> data,config;
        for(auto item:param_config){
            config = config_parse(item[0].c_str(),req->getParameter(item[0]),atoi(item[2].c_str()));
            data.insert(data.end(),config.begin(),config.end());
        }
        callback(22,0x4a,data);
    }
    void imports(const HttpRequestPtr& req,const HttpResponsePtr& resp,
        std::map<boost::asio::ip::tcp::socket*,std::tuple<std::string,bool>>::iterator it,
        std::function<void(int,int,std::vector<unsigned char>&)> callback){
        set_capture(req,resp,it,callback);
        set_sensor(req,resp,it,callback);
        std::ofstream ofs(std::get<0>(it->second)+".csv");
        for(auto item:capture_config){
            ofs<<boost::locale::conv::to_utf<char>(item[0],"utf8")<<"\t"<<req->getParameter(item[0])<<std::endl;
        }
        for(auto item:param_config){
            ofs<<boost::locale::conv::to_utf<char>(item[0],"utf8")<<"\t"<<req->getParameter(item[0])<<std::endl;
        }
        ofs.close();
        resp->setBody(R"({"state":0})");
    }
}